<?php
class Bizprofile_Models_Dao_Pdo_Mysql_Message extends Tomato_Model_Dao
	implements Bizprofile_Models_Interface_Message
{
	public function convert($entity)
	{
		return new Bizprofile_Models_Message($entity); 
	}
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('m' => $this->_prefix . 'enterprise_message'))
					->where('m.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
    	return (null == $row) ? null : new Bizprofile_Models_Message($row);
	}
	
	public function add($message) 
	{
		$this->_conn->insert($this->_prefix . 'enterprise_message', 
							array(
								'fromuser_id' 			=> $message->fromuser_id,
								'fromuser_name'   		=> $message->fromuser_name,
								'fromenterprise_id'		=> $message->fromenterprise_id,
								'fromenterprise_name'	=> $message->fromenterprise_name,
								'title'					=> $message->title,
								'message'				=> $message->message,
								'touserarray'			=> $message->touserarray,
								'toenterprisearray'		=> $message->toenterprisearray,
								'dateline_send'			=> $message->dateline_send,
								'dateline_read'			=> $message->dateline_read
							));
		return $this->_conn->lastInsertId($this->_prefix . 'enterprise_message');
	}
	
	public function update($message) 
	{
		$data = array(
			'dateline_read'			=> $message->dateline_read
		);		 
		return $this->_conn->update($this->_prefix . 'enterprise_message', 
									$data, 
									array(
										'id = ?' => $message->id,
									));
	}
	
	public function find($offset = null, $count = null, $exp = null)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_message'));
		if ($exp) {
			if (isset($exp['fromuser_id'])) {
				$select->where('m.fromuser_id = ?', $exp['fromuser_id']);
			}
			if (isset($exp['fromuser_name'])) {
				$select->where('m.fromuser_name = ?', $exp['fromuser_name']);
			}
			if (isset($exp['fromenterprise_id'])) {
				$select->where('m.fromenterprise_id = ?', $exp['fromenterprise_id']);
			}			
			if (isset($exp['fromenterprise_name'])) {
				$select->where('m.fromenterprise_name = ?', $exp['fromenterprise_name']);
			}			
		}
		if (is_int($offset) && is_int($count)) {
			$select->limit($count, $offset);
		}
		$rs = $select->query()->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function count($exp)
	{
		$select = $this->_conn
						->select()
						->from(array('m' => $this->_prefix . 'enterprise_message'), array('num_messages' => 'COUNT(*)'));
		if ($exp) {
			if (isset($exp['fromuser_id'])) {
				$select->where('m.fromuser_id = ?', $exp['fromuser_id']);
			}
			if (isset($exp['fromuser_name'])) {
				$select->where('m.fromuser_name = ?', $exp['fromuser_name']);
			}
			if (isset($exp['fromenterprise_id'])) {
				$select->where('m.fromenterprise_id = ?', $exp['fromenterprise_id']);
			}			
			if (isset($exp['fromenterprise_name'])) {
				$select->where('m.fromenterprise_name = ?', $exp['fromenterprise_name']);
			}
		}
		return $select->limit(1)->query()->fetch()->num_messages;
	}
}
